<template>
  <div class="question">
    <div class="block">
      <el-card class="box-card" :body-style="{backgroundColor: '#fafafa' }">
        <div slot="header" class="box-head">
          <span class="m-title">答题学习</span>
        </div>
        <div class="title">
          <span>第{{this.num}}题：<br>{{this.ques.cont}}</span>
        </div>
        <div class="option">
          <el-radio-group v-model="type1" class="radio">
            <el-radio v-show="show1" label="A">
              <el-card shadow="hover">
                <span ref="op1">
                  {{this.ques.op1}}
                </span>
              </el-card>
            </el-radio>
            <el-radio v-show="show2" label="B">
              <el-card shadow="hover">
                <span ref="op2">
                  {{this.ques.op2}}
                </span>
              </el-card>
            </el-radio>
            <el-radio v-show="show3" label="C">
              <el-card shadow="hover">
                <span ref="op3">
                  {{this.ques.op3}}
                </span>
              </el-card>
            </el-radio>
            <el-radio v-show="show4" label="D">
              <el-card shadow="hover">
                <span ref="op4">
                  {{this.ques.op4}}
                </span>
              </el-card>
            </el-radio>
          </el-radio-group>
        </div>
        <!--el-button @click="previous()">上一题</el-button-->
        <el-button @click="call()">确  定</el-button>
        <el-button @click="next()">下一题</el-button>
      </el-card>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";

export default {
  data : function() {
    return {
      user: {},
      ques: {},
      total: '',
      num: 1,
      show1: true,
      show2: true,
      show3: true,
      show4: true,
      res: '',
      type1: '',
      type2: '',
      cilck: false,
    }
  },
  mounted() {
    this.load();
  },
  methods: {
    load() {
      if (!sessionStorage.getItem("user")) {
        this.$message({
          showClose: true,
          type: 'error',
          message: '您还未登录，请登录查看个人信息！'
        });
        this.$router.push("/login")//页面跳转
      } else {
        this.user = JSON.parse(sessionStorage.getItem("user"))
        axios.get("api/question/info/page",{
          params: {
            num: this.num,
            size: 1,
          }
        }).then((res) => {
          this.total = res.data.data.total
          this.ques = res.data.data.records[0]
          if(this.ques.op4 == null){
            this.show4 = false;
          }
          if(this.ques.op3 == null){
            this.show3 = false;
          }
        })
      }
    },
    call() {
      if(this.cilck === false) { // 判断是否回答过
        if(this.ques.qtype === 2){ // 判断题目类型
         this.res = this.type2.sort().join('')
          // 若是多选题将选项数组排序并转化成字符串并赋值
        } else {
          this.res = this.type1
          // 单选题直接赋值
        }
        var res = ''
        if(this.res === this.ques.ans) { // 判断是否回答正确
          this.$message({
            showClose: true,
            type: "success",
            message: "回答正确！"
          })
          // 返回正确信息
          res = 1
        } else {
          this.$message({
            showClose: true,
            type: "error",
            message: "回答错误！"
          })
          // 返回错误信息
          res = 0
          if(this.res.includes('A')){
            this.$refs.op1.style.color = '#F56C6C'
            // 错误选择标红色
          }
          if(this.res.includes('B')){
            this.$refs.op2.style.color = '#F56C6C'
          }
          if(this.res.includes('C')){
            this.$refs.op3.style.color = '#F56C6C'
          }
          if(this.res.includes('D')){
            this.$refs.op4.style.color = '#F56C6C'
          }
        }
        if(this.ques.ans.includes('A')){
          this.$refs.op1.style.color = '#67C23A'
          // 正确选择标绿色
        }
        if(this.ques.ans.includes("B")){
          this.$refs.op2.style.color = '#67C23A'
        }
        if(this.ques.ans.includes('C')){
          this.$refs.op3.style.color = '#67C23A'
        }
        if(this.ques.ans.includes('D')){
          this.$refs.op4.style.color = '#67C23A'
        }

        axios.post("api/record", qs.stringify({
          uid: this.user.id,
          qid: this.ques.id,
          type: 1,
          res: res,
        })).then((res) => {
          if(res.data.code !== '0') {
            this.$message({
              showClose: true,
              type: "error",
              message: res.data.msg,
            })
          }
        })
        this.cilck = true // 设置已回答题目
      } else { // 已回答题目
        this.$message({
          showClose: true,
          type: "error",
          message: "已回答过该题！请点击下一题！"
        })
        // 返回错误信息
      }
    },
    get() {
      if(this.num > this.total) {
        this.$message({
          showClose: true,
          type: "error",
          message: "已回答到最后一题啦！请休息一下吧！"
        })
        this.num -= 1
        this.$router.push("/learn")
      }

      axios.get("api/question/info/page",{
        params: {
          num: this.num,
          size: 1,
        }
      }).then((res) => {
        this.total = res.data.data.total
        this.ques = res.data.data.records[0]
        if(this.ques.op3 == null || this.ques.op3 === ''){
          this.show3 = false;
        } else {
          this.show3 = true;
        }
        if(this.ques.op4 == null || this.ques.op4 === ''){
          this.show4 = false;
        } else {
          this.show4 = true;
        }
      })

      this.$refs.op1.style.color = '#000000'
      this.$refs.op2.style.color = '#000000'
      this.$refs.op3.style.color = '#000000'
      this.$refs.op4.style.color = '#000000'
      this.type1 = ''
      this.cilck = false // 设置已回答题目
    },
    previous() {
      this.num -= 1
      this.get()
    },
    next() {
      if(this.cilck === false) {
        this.$message({
          showClose: true,
          type: "error",
          message: "还没回答当前题目呢！"
        })
      } else {
        this.num += 1
        this.get()
      }
    },
  }
}
</script>

<style scoped>
.question{
  width: 100%;
  min-height: 666px;
}

.block{
  width: 80%;
  padding-top: 20px;
  position: relative;
  left: 50%;
  transform: translate(-50%, 0%);
  /*box-shadow: 0 0 8px rgba(0, 0, 0, 0.6);*/
}

.box-card {
  background-color: #354e66;
}

.box-head {
  justify-content: center;
}

.m-title {
  color: #f9bf4b;
  font-size: 18px;
  font-weight: bold;
  padding-top: 20px;
}

.title {
  position: relative;
  left: 50%;
  transform: translate(-45%, 0%);
  padding-top: 20px;
  text-align: left;
  width: 800px;
  min-height: 100px;
}

.option {
  min-height: 322.4px;
}

/deep/ .el-radio__label {
  width: 500px;
}

.el-radio {
  display: flex;
  align-items: center;
  text-align: left;
  margin: 20px 0;
}

.el-button {
  width: 120px;
  height: 40px;
  color: #f9bf4b;
  font-size: 17px;
  font-weight: bold;
  background: #354e66;
  margin-bottom: 20px;
}
</style>